package common.concurrency.baeldung.thread;

import java.util.concurrent.LinkedBlockingQueue;

/**
 * @author zhang hao dong
 * @date 2025-06-06 10:21
 */
public class MyBlockingQueue {
    private int maxSize;
    private LinkedBlockingQueue<Object> storage;

    public MyBlockingQueue(int maxSize) {
        this.maxSize = maxSize;
        storage = new LinkedBlockingQueue<>();
    }

    public synchronized void put() throws InterruptedException {
        while (storage.size() >= maxSize) {
            wait();
        }
        storage.add(new Object());
        notifyAll();
    }

    public synchronized void take() throws InterruptedException {
        while (storage.size() == 0) {
            wait();
        }
        System.out.println(storage.remove());
        notifyAll();
    }
}
